package com.bt.service.impl;

import com.bt.dto.SysRoleDTO;
import com.bt.entity.SysMenu;
import com.bt.entity.SysRole;
import com.bt.entity.SysUser;
import com.bt.mapper.SysMenuMapper;
import com.bt.mapper.SysRoleMenuMapper;
import com.bt.mapper.SysUserMapper;
import com.bt.vo.SysMenuVO;
import com.bt.vo.SysRoleVO;
import org.apache.commons.lang.StringUtils;
import com.bt.dto.SysRolePageQueryDTO;
import com.bt.mapper.SysRoleMapper;
import com.bt.result.PageResult;
import com.bt.service.SysRoleService;
import com.bt.vo.SysRolePageQueryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-05-02 21:19
 */
@Service
@Slf4j
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysUserMapper sysUserMapper;
    /**
     * 角色列表分页
     *
     * @param sysRolePageQueryDTO
     * @return
     */

    public PageResult list(SysRolePageQueryDTO sysRolePageQueryDTO) {
        //查询ydd_advimgd数据库的总条数
        int count = sysRoleMapper.getCount();
        //获取分页的起始索引
        int start = (sysRolePageQueryDTO.getPage() - 1) * sysRolePageQueryDTO.getLimit();
        //获取每页展示的数量
        int length = sysRolePageQueryDTO.getLimit();
        //获取展示的总页数
        int totalPages = 0;
        if(length>0) {
            if (count % 10 == 0) {
                totalPages = count / length;
            } else {
                totalPages = (count / length) + 1;
            }
        }

        //获取当前页码
        int currentPage = sysRolePageQueryDTO.getPage();
        //获取模糊查询条件
        String name = sysRolePageQueryDTO.getName();


        //获取当前页数据集合
        List<SysRolePageQueryVO> sysRolePageQueryVOList = sysRoleMapper.list(start, length, name);
        //用来存放菜单编号
        /*   List<String> list = new ArrayList();*/

        for (SysRolePageQueryVO sysRolePageQueryVO : sysRolePageQueryVOList) {
            String roleId = sysRolePageQueryVO.getId();
            sysRolePageQueryVO.setRoleId(roleId);
            List<String> sysRoleMenuVOList = sysRoleMenuMapper.findMenuId(roleId);

            String roleMenu = StringUtils.join(sysRoleMenuVOList, ",");
            sysRolePageQueryVO.setRoleMenus(roleMenu);

        }

        PageResult pageResult = PageResult.builder()
                .count(count)
                .totalPages(totalPages)
                .pageSize(length)
                .currentPage(currentPage)
                .data(sysRolePageQueryVOList)
                .build();
        return pageResult;
    }
    /**
     *  角色添加
     * @param sysRoleDTO
     */
    @Transactional
    @Override
    public void add(SysRoleDTO sysRoleDTO) {
        String id = UUID.randomUUID().toString();
        String name = sysRoleDTO.getName();
        String description = sysRoleDTO.getDescription();
        List<String> menuids = sysRoleDTO.getMenuids();
        Date createDate = new Date();
        Date updateDate = new Date();
        SysRole sysRole = SysRole.builder()
                .id(id)
                .name(name)
                .description(description)
                .createDate(createDate)
                .updateDate(updateDate)
                .build();
        sysRoleMapper.add(sysRole);

        for (String menuid : menuids) {
            sysRoleMenuMapper.addMenuid(id,menuid);
        }


    }

    /**
     * 角色详情
     *
     * @param id
     * @return
     */
    @Override
    public SysRoleVO rowInfo(String id) {

        //根据角色id查询角色表sys_role
        SysRole sysRole = sysRoleMapper.rowInfo(id);
        //根据角色id查询sys_role_menu表
        List<String> menuIds = sysRoleMenuMapper.findMenuId(id);
        //sysMenuVOList用来存储SysMenuVO对象
        List<SysMenuVO> sysMenuVOList = new ArrayList<>();

        for (String menuId : menuIds) {
            SysMenuVO sysMenuVO = new SysMenuVO();
            SysMenu sysMenu = sysMenuMapper.getById(menuId);
            //通过sysMenu.getId()查询子标签
            List<SysMenuVO> sysMenuVOListZi = getAll(sysMenu.getId());
            BeanUtils.copyProperties(sysMenu,sysMenuVO);
            sysMenuVO.setChildren(sysMenuVOListZi);
            sysMenuVOList.add(sysMenuVO);

        }
        SysRoleVO sysRoleVO = new SysRoleVO();
        BeanUtils.copyProperties(sysRole,sysRoleVO);
        sysRoleVO.setAuthList(sysMenuVOList);
        return sysRoleVO;





    }
    public List<SysMenuVO> getAll(Integer pid){
        List<SysMenuVO> sysMenuVOList =  sysMenuMapper.getByPid(pid);
        for (SysMenuVO sysMenuVO : sysMenuVOList) {
            Integer id = sysMenuVO.getId();
            List<SysMenuVO>  childer =  getAll(id);
            sysMenuVO.setChildren(childer);
        }
        return sysMenuVOList;
    }

    /**
     * 查询角色列表全部
     * @return
     */
    @Override
    public List<SysRoleVO> allData() {
        List<SysRoleVO> sysRoleVOList =    sysRoleMapper.allData();
        return sysRoleVOList;

    }

    /**
     * 角色删除
     * @param id
     */
    @Transactional
    @Override
    public void del(String id) {
        //判断角色是否关联了用户表
         String roleId =   "["+id+"]";
         //根据roleId查找用户表数据
         List<SysUser> sysUserList =   sysUserMapper.getById(roleId);

         if(sysUserList == null | sysUserList.size()==0){
             //根据角色id删除角色数据
             sysRoleMapper.del(id);
             //根据角色id删除对应菜单的数据
             sysRoleMenuMapper.del(id);
         }

    }

    /**
     *  角色修改
     * @param sysRoleDTO
     */
    @Transactional
    @Override
    public void update(SysRoleDTO sysRoleDTO) {
           sysRoleMapper.update(sysRoleDTO);
        List<String> menuids = sysRoleDTO.getMenuids();
        //删除关联的数据
        sysRoleMenuMapper.del(sysRoleDTO.getId());

        for (String menuid : menuids) {
            //添加修改后的数据
            sysRoleMenuMapper.addMenuid(sysRoleDTO.getId(),menuid);
        }



    }
}
